package com.hzh.cusviewdemo.Adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.hzh.cusviewdemo.Activity.MusicActivity;
import com.hzh.cusviewdemo.Bean.MusicSearchData;
import com.hzh.cusviewdemo.Bean.MyMusicData;
import com.hzh.cusviewdemo.Bean.MySongMenu;
import com.hzh.cusviewdemo.Music.Constant;
import com.hzh.cusviewdemo.Music.MyMusicUtil;
import com.hzh.cusviewdemo.R;
import com.hzh.cusviewdemo.Util.Config;
import com.hzh.cusviewdemo.Util.DialogUtils;
import com.hzh.cusviewdemo.Util.SugarUtils;

import java.util.List;

public class SearchResultAdapter extends BaseLoadMoreDataAdapter<MusicSearchData> {

    private String searchContent = "";  // 搜索的内容
    private int isPlayingPos = -1;

    public OnListenMusicListener mListener;

    public interface OnListenMusicListener {
        void onListenMusic(MusicSearchData musicSearchData, int pos, boolean isLike);
    }

    public void setOnListenMusicListener(OnListenMusicListener listener) {
        this.mListener = listener;
    }

    public SearchResultAdapter(Context context, List<MusicSearchData> dataList) {
        super(context, dataList);
    }

    @Override
    public void bindData(BaseRecyclerViewHolder holder, int position, MusicSearchData item) {
        MusicSearchData musicSearchData = getData().get(position);
        String musicId = musicSearchData.getId();
        Log.e(Config.TAG, "搜索音乐 - Z - 歌曲SID -> " + musicId);
        MyMusicData musicDataByRid = SugarUtils.findMusicDataByRidOrSid(musicId);
        if (musicDataByRid != null) {
            if (musicDataByRid.getMenu().equals("我喜欢的音乐")) {
                // 置红色
                ImageView isr_like = holder.findViewById(R.id.isr_like);
                isr_like.setImageResource(R.drawable.ic_like_fill);
            } else {
                ImageView isr_like = holder.findViewById(R.id.isr_like);
                isr_like.setImageResource(R.drawable.ic_like_black);
            }
        } else {
            ImageView isr_like = holder.findViewById(R.id.isr_like);
            isr_like.setImageResource(R.drawable.ic_like_black);
        }
        holder.setText(R.id.isr_song_name, item.getName());
        holder.setText(R.id.isr_song_owner, item.getArtist());
        holder.setGone(R.id.isr_tv_song_type, position != 0);
        if (isPlayingPos != -1) {
            // 播放的是
            holder.setGone(R.id.isr_play_load, position != isPlayingPos);
        }
        if (position == 0) {
            holder.setGone(R.id.isr_tv_vip_type, false);
            holder.setText(R.id.isr_song_level, "Hi-Res");
        } else {
            holder.setGone(R.id.isr_tv_vip_type, item.getName().length() > 12);
            holder.setText(R.id.isr_song_level, "SQ");
        }
        holder.setOnClick(R.id.isr_linear_music, v -> {
            if (musicSearchData == null) {
                DialogUtils.showErrorToast("当前歌曲异常，无法收听~");
                return;
            }
            if (musicSearchData.getId().equals("")) {
                DialogUtils.showErrorToast("当前歌曲异常，无法收听~");
                return;
            }
            if (mListener == null) {
                DialogUtils.showErrorToast("操作异常，请重新进入搜索页面搜索~");
                return;
            }
            mListener.onListenMusic(musicSearchData, position, false);
        });
        holder.setOnClick(R.id.isr_like, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 把歌曲添加到我喜欢
                MusicSearchData musicSearchData = getData().get(position);
                String musicId = musicSearchData.getId();
                Log.e(Config.TAG, "搜索音乐 - X - 歌曲SID -> " + musicId);
                MyMusicData musicDataByRidOrSid = SugarUtils.findMusicDataByRidOrSid(musicId);
                if (musicDataByRidOrSid != null) {
                    if (musicDataByRidOrSid.getMenu().equals("我喜欢的音乐")) {
                        // 已经喜欢过该歌曲
                        // 更新到全部歌曲
                        musicDataByRidOrSid.setMenu("全部歌曲");
                        musicDataByRidOrSid.save();

                        MySongMenu menu = SugarUtils.getSongMenuByName("我喜欢的音乐");
                        if (menu != null) {
                            menu.setNum(menu.getNum() - 1);
                            List<MyMusicData> likeMusicDataList = SugarUtils.findMusicDataByMenu("我喜欢的音乐");
                            if (!likeMusicDataList.isEmpty()) {
                                menu.setPic(likeMusicDataList.get(0).getPic());
                            } else {
                                menu.setPic("");
                            }
                            menu.save();
                        }

                        MySongMenu allMenu = SugarUtils.getSongMenuByName("全部歌曲");
                        if (allMenu != null) {
                            allMenu.setNum(allMenu.getNum() + 1);
                            allMenu.setPic(MyMusicUtil.getStringShared(Constant.KEY_PIC));
                            allMenu.save();
                        }

                        // 置红色
                        ImageView isr_like = holder.findViewById(R.id.isr_like);
                        isr_like.setImageResource(R.drawable.ic_like_black);

                        // 通知首页刷新
                        Intent musicIntent = new Intent(MusicActivity.ACTION_UPDATE_UI_MUSIC);
                        musicIntent.putExtra(Constant.STATUS, Constant.STATUS_UPDATE_MENU_LIST);
                        getContext().sendBroadcast(musicIntent);
                    } else {
                        // 更新到我喜欢
                        musicDataByRidOrSid.setMenu("我喜欢的音乐");
                        musicDataByRidOrSid.save();

                        MySongMenu menu = SugarUtils.getSongMenuByName("我喜欢的音乐");
                        if (menu != null) {
                            menu.setNum(menu.getNum() + 1);
                            menu.setPic(MyMusicUtil.getStringShared(Constant.KEY_PIC));
                            menu.save();
                        }

                        MySongMenu allMenu = SugarUtils.getSongMenuByName("全部歌曲");
                        if (allMenu != null) {
                            allMenu.setNum(allMenu.getNum() - 1);
                            List<MyMusicData> unLikeMusicDataList = SugarUtils.findMusicDataByMenu("全部歌曲");
                            if (!unLikeMusicDataList.isEmpty()) {
                                allMenu.setPic(unLikeMusicDataList.get(0).getPic());
                            } else {
                                allMenu.setPic("");
                            }
                            allMenu.save();
                        }

                        // 置红色
                        ImageView isr_like = holder.findViewById(R.id.isr_like);
                        isr_like.setImageResource(R.drawable.ic_like_fill);

                        // 通知首页刷新
                        Intent musicIntent = new Intent(MusicActivity.ACTION_UPDATE_UI_MUSIC);
                        musicIntent.putExtra(Constant.STATUS, Constant.STATUS_UPDATE_MENU_LIST);
                        getContext().sendBroadcast(musicIntent);
                    }

                    Intent updateUiIntent = new Intent(MusicActivity.ACTION_UPDATE_UI_MUSIC);
                    updateUiIntent.putExtra(Constant.STATUS, Constant.STATUS_REFRESH_LIKE_FROM_SEARCH_LIST);
                    getContext().sendBroadcast(updateUiIntent);
                } else {
                    // 置红色
                    ImageView isr_like = holder.findViewById(R.id.isr_like);
                    isr_like.setImageResource(R.drawable.ic_like_fill);

                    mListener.onListenMusic(musicSearchData, position, true);
                }
            }
        });
    }

    @Override
    public int getItemLayoutId(int viewType) {
        return R.layout.item_search_result;
    }

    public void setSearchContent(String searchContent) {
        this.searchContent = searchContent;
    }

    /**
     * 通知适配器音乐播放状态更新
     *
     * @param pos
     */
    public void setPlaying(int pos) {
//        isPlayingPos = pos;
//        notifyItemChanged(pos);
        notifyDataSetChanged();
    }

    public void resetAdapter() {
        isPlayingPos = -1;
        searchContent = "";
    }
}
